package com.shop2cn.iapi.couponmanage.testcase.old;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.couponmanage.parameter.CreateBagBean;
import com.shop2cn.iapi.couponmanage.parameter.CreateBean;
import com.shop2cn.iapi.couponmanage.parameter.CreateCouponBean;
import com.shop2cn.iapi.couponmanage.parameter.createTaskByFileBean;
import com.shop2cn.iapi.couponmanage.parameter.createTaskByUserIdBean;
import com.shop2cn.iapi.couponmanage.parameter.executeTaskBean;
import com.shop2cn.iapi.couponmanage.service.CreateBagCall;
import com.shop2cn.iapi.couponmanage.service.CreateCall;
import com.shop2cn.iapi.couponmanage.service.CreateCouponCall;
import com.shop2cn.iapi.couponmanage.service.createTaskByFileCall;
import com.shop2cn.iapi.couponmanage.service.createTaskByUserIdCall;
import com.shop2cn.iapi.couponmanage.service.executeTaskCall;
import com.ymatou.iapi.fileupload.service.UploadCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponShop2cnCallService;
import com.ymttest.business.service.CouponShop2cnMCallService;
import com.ymttest.database.model.coupon;
import com.ymttest.database.model.couponbatch;
import com.ymttest.database.model.couponsendsubtask;
import com.ymttest.database.model.couponsendtask;
import com.ymttest.database.sqlwapper.couponWapper;
import com.ymttest.database.sqlwapper.couponbatchWapper;
import com.ymttest.database.sqlwapper.couponsendsubtaskWapper;
import com.ymttest.database.sqlwapper.couponsendtaskWapper;
/**
*消息生总执行子任务(生成优惠(套)券，生成兑换码)
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_executeTask{
	private static executeTaskBean executetaskBean;
	private static executeTaskCall executetaskCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("消息生总执行子任务(生成优惠(套)券，生成兑换码)");
	}

	@Before
	public void caseUp() {
		executetaskBean=new executeTaskBean();
		executetaskCall=new executeTaskCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(executetaskCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_executeTask_001() {
		Logger.start(true,"消息生总执行子任务(生成优惠(套)券，生成兑换码)-套券");
		try {
			CouponShop2cnMCallService couponCallService = new CouponShop2cnMCallService();
			CreateBagBean createBagBean = couponCallService.setDefaultPlatformCreateCouponBagBeanForRecieve();
			CreateBagCall createBagCall = couponCallService.createPlatformCouponBag(createBagBean);
			
		    setCreateTaskByUserIdBeanForPlatform(createBagCall.getbagCode(),2);
			
			couponsendtask task=new couponsendtaskWapper().selectByCouponBatchCode(createBagCall.getbagCode()).get(0);
			couponsendsubtask sub=new couponsendsubtaskWapper().selectcouponsendsubtaskByTaskId(task.getTaskId()).get(0);
			executetaskBean.setSubTaskId(sub.getSubTaskId());
			executetaskCall.setData(executetaskBean);
			executetaskCall.callService();		
			
			couponsendsubtask sub2=new couponsendsubtaskWapper().selectcouponsendsubtaskByTaskId(task.getTaskId()).get(0);
			Logger.verifyEquals("ok", executetaskCall.getReturnData(), "验证返回ok");
			Logger.verifyEquals(4, sub2.getStatus(), "验证数据库couponsendsubtask表中的status=4,成功");
			Logger.comment("TaskId:"+sub2.getTaskId());
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_executeTask_002() {
		Logger.start(true,"消息生总执行子任务(生成优惠(套)券，生成兑换码)-随机兑换码");
		try {
			CouponShop2cnMCallService couponMCallService = new CouponShop2cnMCallService();
			CreateCouponBean createCouponBean = couponMCallService.setDefaultPlatformCreateCouponBeanForConversion();
			CreateCouponCall createCouponCall = couponMCallService.createPlatformCoupon(createCouponBean);
			
			List<String> batchCodes = new ArrayList<>();
			batchCodes.add(createCouponCall.getBatchCode());
			CreateBean createBean = couponMCallService.setDefaultRandomCreateBean(batchCodes);
			CreateCall createCall = couponMCallService.create(createBean);

			couponsendtask task=new couponsendtaskWapper().selectByCouponBatchCode(createCall.getJsonObject().get("data").getAsJsonObject().get("conversionBillId").getAsString()).get(0);
			couponsendsubtask sub=new couponsendsubtaskWapper().selectcouponsendsubtaskByTaskId(task.getTaskId()).get(0);
			executetaskBean.setSubTaskId(sub.getSubTaskId());
			executetaskCall.setData(executetaskBean);
			executetaskCall.callService();		
			
			couponsendsubtask sub2=new couponsendsubtaskWapper().selectcouponsendsubtaskByTaskId(task.getTaskId()).get(0);
			Logger.verifyEquals("ok", executetaskCall.getReturnData(), "验证返回ok");
			Logger.verifyEquals(4, sub2.getStatus(), "验证数据库couponsendsubtask表中的status=4,成功");
			Logger.comment("TaskId:"+sub2.getTaskId());
			Logger.comment("BatchCode:"+createCall.getJsonObject().get("data").getAsJsonObject().get("conversionBillId").getAsString());
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_executeTask_003() {
		Logger.start(true,"消息生总执行子任务(生成优惠(套)券，生成兑换码)-SubTaskId=0");
		try {
		  //executetaskBean.setSubTaskId(0);
			executetaskCall.setData(executetaskBean);
			executetaskCall.callService();		

			Logger.verifyEquals("ok", executetaskCall.getReturnData(), "验证返回ok");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_executeTask_004() {
		Logger.start(true,"消息生总执行子任务(生成优惠(套)券，生成兑换码)-SubTaskId不存在");
		try {
			CouponShop2cnMCallService couponCallService = new CouponShop2cnMCallService();
			CreateBagBean createBagBean = couponCallService.setDefaultPlatformCreateCouponBagBeanForRecieve();
			CreateBagCall createBagCall = couponCallService.createPlatformCouponBag(createBagBean);
			
		    setCreateTaskByUserIdBeanForPlatform(createBagCall.getbagCode(),2);
			
			couponsendtask task=new couponsendtaskWapper().selectByCouponBatchCode(createBagCall.getbagCode()).get(0);
			couponsendsubtask sub=new couponsendsubtaskWapper().selectcouponsendsubtaskByTaskId(task.getTaskId()).get(0);
			executetaskBean.setSubTaskId(sub.getSubTaskId()+1111);
			executetaskCall.setData(executetaskBean);
			executetaskCall.callService();		

			Logger.verifyEquals("ok", executetaskCall.getReturnData(), "验证返回ok");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_executeTask_005() {
		Logger.start(true,"消息生总执行子任务(生成优惠(套)券，生成兑换码)-生成私有券-生效时间是相对时间-小时");
		try {
			CouponShop2cnMCallService couponMCallService = new CouponShop2cnMCallService();
			CreateCouponBean createCouponBean = couponMCallService.setDefaultPlatformCreateCouponBeanForRecieve();
			createCouponBean.setCouponNum(100);
			
			createCouponBean.setEffectiveType(1);//0：绝对有效期 1：相对有效期
			createCouponBean.setEffectiveTimeType(1);
			createCouponBean.setEffectiveDays(2);
			CreateCouponCall createCouponCall = couponMCallService.createPlatformCoupon(createCouponBean);
			
			createTaskByFileBean createTaskByFileBean=new createTaskByFileBean();
			createTaskByFileCall createTaskByFileCall=new createTaskByFileCall();
			UploadCall uploadcall=couponMCallService.fileUpload("userId1.txt");
			List<String> userIds= new Ts_createTaskByFile().readFile("userId1.txt");
			createTaskByFileBean.setFileUrl(uploadcall.getFileUrl());
			createTaskByFileBean.setBatchType(1);
			createTaskByFileBean.setBatchCode(createCouponCall.getBatchCode());
			createTaskByFileBean.setCreaterId(11);
			createTaskByFileBean.setCreaterName("自动化测试");
			createTaskByFileBean.setNotify(false);
			createTaskByFileBean.setUserQuantity(3);
			createTaskByFileCall.setData(createTaskByFileBean);
			createTaskByFileCall.callService();
			
             Thread.sleep(10000);
             Logger.comment("createTaskByFileCall.getTaskId():"+createTaskByFileCall.getTaskId());
			couponsendsubtask sub=new couponsendsubtaskWapper().selectcouponsendsubtaskByTaskId(createTaskByFileCall.getTaskId()).get(0);
			
			executetaskBean.setSubTaskId(sub.getSubTaskId());
			
			executetaskCall.setData(executetaskBean);
			executetaskCall.callService();		
			
			couponsendsubtask sub2=new couponsendsubtaskWapper().selectcouponsendsubtaskByTaskId(createTaskByFileCall.getTaskId()).get(0);
			Logger.verifyEquals("ok", executetaskCall.getReturnData(), "验证返回ok");
			Logger.verifyEquals(4, sub2.getStatus(), "验证数据库couponsendsubtask表中的status=4,成功");
			//检查优惠券表
			couponbatch batch=new couponbatchWapper().selectBybatchCode(createCouponCall.getBatchCode()).get(0);
			List<coupon> clist=new couponWapper().selectByBatchId(batch.getBatchId());
			for(coupon c:clist)
			{
				Logger.verifyEquals(sub2.getSuccessNum(), clist.size(), "验证生成优惠券数量");
				Logger.verifyEquals(createCouponBean.getEffectiveDays(),
						(int)(c.getValidEnd().getTime()-c.getValidStart().getTime())/ (1000 * 60 * 60), 
						"验证生成优惠券有效开始时间与结束时间相隔"+createCouponBean.getEffectiveDays()+"小时");
			}
			Logger.comment("TaskId:"+sub2.getTaskId());
			Logger.comment("BatchCode:"+createCouponCall.getBatchCode());
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_executeTask_006() {
		Logger.start(true,"消息生总执行子任务(生成优惠(套)券，生成兑换码)-生成私有券-生效时间是相对时间-天");
		try {
			CouponShop2cnMCallService couponMCallService = new CouponShop2cnMCallService();
			CreateCouponBean createCouponBean = couponMCallService.setDefaultPlatformCreateCouponBeanForRecieve();
			createCouponBean.setCouponNum(100);
			
			createCouponBean.setEffectiveType(1);//0：绝对有效期 1：相对有效期
			createCouponBean.setEffectiveTimeType(0);
			createCouponBean.setEffectiveDays(2);
			CreateCouponCall createCouponCall = couponMCallService.createPlatformCoupon(createCouponBean);
			
			createTaskByFileBean createTaskByFileBean=new createTaskByFileBean();
			createTaskByFileCall createTaskByFileCall=new createTaskByFileCall();
			UploadCall uploadcall=couponMCallService.fileUpload("userId1.txt");
			List<String> userIds= new Ts_createTaskByFile().readFile("userId1.txt");
			createTaskByFileBean.setFileUrl(uploadcall.getFileUrl());
			createTaskByFileBean.setBatchType(1);
			createTaskByFileBean.setBatchCode(createCouponCall.getBatchCode());
			createTaskByFileBean.setCreaterId(11);
			createTaskByFileBean.setCreaterName("自动化测试");
			createTaskByFileBean.setNotify(false);
			createTaskByFileBean.setUserQuantity(3);
			createTaskByFileCall.setData(createTaskByFileBean);
			createTaskByFileCall.callService();
			
             Thread.sleep(10000);
             Logger.comment("createTaskByFileCall.getTaskId():"+createTaskByFileCall.getTaskId());
			couponsendsubtask sub=new couponsendsubtaskWapper().selectcouponsendsubtaskByTaskId(createTaskByFileCall.getTaskId()).get(0);
			
			executetaskBean.setSubTaskId(sub.getSubTaskId());
			
			executetaskCall.setData(executetaskBean);
			executetaskCall.callService();		
			
			couponsendsubtask sub2=new couponsendsubtaskWapper().selectcouponsendsubtaskByTaskId(createTaskByFileCall.getTaskId()).get(0);
			Logger.verifyEquals("ok", executetaskCall.getReturnData(), "验证返回ok");
			Logger.verifyEquals(4, sub2.getStatus(), "验证数据库couponsendsubtask表中的status=4,成功");
			//检查优惠券表
			couponbatch batch=new couponbatchWapper().selectBybatchCode(createCouponCall.getBatchCode()).get(0);
			List<coupon> clist=new couponWapper().selectByBatchId(batch.getBatchId());
			for(coupon c:clist)
			{
				Logger.verifyEquals(sub2.getSuccessNum(), clist.size(), "验证生成优惠券数量");
				Logger.verifyEquals(createCouponBean.getEffectiveDays(),
						(int)(c.getValidEnd().getTime()-c.getValidStart().getTime())/ (1000 * 60 * 60 * 24), 
						"验证生成优惠券有效开始时间与结束时间相隔"+createCouponBean.getEffectiveDays()+"天");
			}
			Logger.comment("TaskId:"+sub2.getTaskId());
			Logger.comment("BatchCode:"+createCouponCall.getBatchCode());
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_executeTask_006_1() {
		Logger.start(true,"消息生总执行子任务(生成优惠(套)券，生成兑换码)-生成私有券-全球优选-生效时间是相对时间-天");
		try {
			CouponShop2cnMCallService couponMCallService = new CouponShop2cnMCallService();
			CreateCouponBean createCouponBean = couponMCallService.setDefaultPlatformCreateCouponBeanForRecieve();
			createCouponBean.setCouponNum(100);
			
			createCouponBean.setEffectiveType(1);//0：绝对有效期 1：相对有效期
			createCouponBean.setEffectiveTimeType(0);
			createCouponBean.setEffectiveDays(2);
			CreateCouponCall createCouponCall = couponMCallService.createPlatformCoupon(createCouponBean);
			//分销 修改platformSource=1
			new couponbatchWapper().setPlatformSource(1, createCouponCall.getBatchCode());
			
			createTaskByFileBean createTaskByFileBean=new createTaskByFileBean();
			createTaskByFileCall createTaskByFileCall=new createTaskByFileCall();
			UploadCall uploadcall=couponMCallService.fileUpload("userId1.txt");
			List<String> userIds= new Ts_createTaskByFile().readFile("userId1.txt");
			createTaskByFileBean.setFileUrl(uploadcall.getFileUrl());
			createTaskByFileBean.setBatchType(1);
			createTaskByFileBean.setBatchCode(createCouponCall.getBatchCode());
			createTaskByFileBean.setCreaterId(11);
			createTaskByFileBean.setCreaterName("自动化测试");
			createTaskByFileBean.setNotify(false);
			createTaskByFileBean.setUserQuantity(3);
			createTaskByFileCall.setData(createTaskByFileBean);
			createTaskByFileCall.callService();
			
             Thread.sleep(10000);
             Logger.comment("createTaskByFileCall.getTaskId():"+createTaskByFileCall.getTaskId());
			couponsendsubtask sub=new couponsendsubtaskWapper().selectcouponsendsubtaskByTaskId(createTaskByFileCall.getTaskId()).get(0);
			
			executetaskBean.setSubTaskId(sub.getSubTaskId());
			
			executetaskCall.setData(executetaskBean);
			executetaskCall.callService();		
			
			couponsendsubtask sub2=new couponsendsubtaskWapper().selectcouponsendsubtaskByTaskId(createTaskByFileCall.getTaskId()).get(0);
			Logger.verifyEquals("ok", executetaskCall.getReturnData(), "验证返回ok");
			Logger.verifyEquals(4, sub2.getStatus(), "验证数据库couponsendsubtask表中的status=4,成功");
			//检查优惠券表
			couponbatch batch=new couponbatchWapper().selectBybatchCode(createCouponCall.getBatchCode()).get(0);
			List<coupon> clist=new couponWapper().selectByBatchId(batch.getBatchId());
			for(coupon c:clist)
			{
				Logger.verifyEquals(sub2.getSuccessNum(), clist.size(), "验证生成优惠券数量");
				Logger.verifyEquals(createCouponBean.getEffectiveDays(),
						(int)(c.getValidEnd().getTime()-c.getValidStart().getTime())/ (1000 * 60 * 60 * 24), 
						"验证生成优惠券有效开始时间与结束时间相隔"+createCouponBean.getEffectiveDays()+"天");
			}
			Logger.comment("TaskId:"+sub2.getTaskId());
			Logger.comment("BatchCode:"+createCouponCall.getBatchCode());
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_executeTask_007() {
		Logger.start(true,"消息生总执行子任务(生成优惠(套)券，生成兑换码)-生成私有券-生效时间是绝对时间");
		try {
			CouponShop2cnMCallService couponMCallService = new CouponShop2cnMCallService();
			CreateCouponBean createCouponBean = couponMCallService.setDefaultPlatformCreateCouponBeanForRecieve();
			createCouponBean.setCouponNum(100);
			
			createCouponBean.setEffectiveType(0);//0：绝对有效期 1：相对有效期
			CreateCouponCall createCouponCall = couponMCallService.createPlatformCoupon(createCouponBean);
			
			createTaskByFileBean createTaskByFileBean=new createTaskByFileBean();
			createTaskByFileCall createTaskByFileCall=new createTaskByFileCall();
			UploadCall uploadcall=couponMCallService.fileUpload("userId1.txt");
			List<String> userIds= new Ts_createTaskByFile().readFile("userId1.txt");
			createTaskByFileBean.setFileUrl(uploadcall.getFileUrl());
			createTaskByFileBean.setBatchType(1);
			createTaskByFileBean.setBatchCode(createCouponCall.getBatchCode());
			createTaskByFileBean.setCreaterId(11);
			createTaskByFileBean.setCreaterName("自动化测试");
			createTaskByFileBean.setNotify(false);
			createTaskByFileBean.setUserQuantity(3);
			createTaskByFileCall.setData(createTaskByFileBean);
			createTaskByFileCall.callService();
			
             Thread.sleep(10000);
             Logger.comment("createTaskByFileCall.getTaskId():"+createTaskByFileCall.getTaskId());
			couponsendsubtask sub=new couponsendsubtaskWapper().selectcouponsendsubtaskByTaskId(createTaskByFileCall.getTaskId()).get(0);
			
			executetaskBean.setSubTaskId(sub.getSubTaskId());
			
			executetaskCall.setData(executetaskBean);
			executetaskCall.callService();		
			
			couponsendsubtask sub2=new couponsendsubtaskWapper().selectcouponsendsubtaskByTaskId(createTaskByFileCall.getTaskId()).get(0);
			Logger.verifyEquals("ok", executetaskCall.getReturnData(), "验证返回ok");
			Logger.verifyEquals(4, sub2.getStatus(), "验证数据库couponsendsubtask表中的status=4,成功");
			//检查优惠券表
			couponbatch batch=new couponbatchWapper().selectBybatchCode(createCouponCall.getBatchCode()).get(0);
			List<coupon> clist=new couponWapper().selectByBatchId(batch.getBatchId());
			for(coupon c:clist)
			{
				Logger.verifyEquals(sub2.getSuccessNum(), clist.size(), "验证生成优惠券数量");
				Logger.verifyEquals(batch.getAcquireStartTime(),c.getValidStart(), "验证生成优惠券有效开始时间与 批次有效开始时间一致");
				Logger.verifyEquals(batch.getAcquireEndTime(),c.getValidEnd(), "验证生成优惠券有效结束时间与 批次有效结束时间一致");
			}
			Logger.comment("TaskId:"+sub2.getTaskId());
			Logger.comment("BatchCode:"+createCouponCall.getBatchCode());
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	private void setCreateTaskByUserIdBeanForPlatform(String batchCode,int batchType ) throws ClientProtocolException, IOException
	{
		createTaskByUserIdBean createTaskByUserIdBean=new createTaskByUserIdBean();
		createTaskByUserIdCall call = new createTaskByUserIdCall();
		List<Integer> userIds=new ArrayList<Integer>();
		userIds.add(11);
		createTaskByUserIdBean.setUserIds(userIds);
		createTaskByUserIdBean.setBatchType(batchType);//1:生成私有券 2:生成短码 3:生成兑换码 4:生成套券
		createTaskByUserIdBean.setBatchCode(batchCode);
		createTaskByUserIdBean.setCreaterId(22);
		createTaskByUserIdBean.setCreaterName("autoTest");
		createTaskByUserIdBean.setNotify(false);
		call.setData(createTaskByUserIdBean);
		call.callService();
	}
}